﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.Pages
{
    public partial class BSCTreeView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 0, NestedLevel = 0 });
                RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 1, NestedLevel = 0 });
                RadTreeList1.ExpandedIndexes.Add(new TreeListHierarchyIndex { LevelIndex = 2, NestedLevel = 0 });
                //(RadTreeList1.FindControl("img") as Image).ImageUrl = "~/Images/Symbols-Delete-icon.png";
            }
        }
        protected void RadTreeList1_NeedDataSource(object sender, TreeListNeedDataSourceEventArgs e)
        {
            RadTreeList1.DataSource = MyData.GetData();
        }

        protected void RadTreeList1_DataBound(object sender, EventArgs e)
        {
            //if (e.Item is TreeListDataItem)
            //{

            //}
        }

        protected void RadTreeList1_ItemDataBound(object sender, TreeListItemDataBoundEventArgs e)
        {
            if (e.Item is TreeListDataItem)
            {
                TreeListDataItem item = e.Item as TreeListDataItem;
                if (item.HierarchyIndex.NestedLevel == 1)
                {
                    (item.FindControl("img") as Image).ImageUrl = "~/Images/commercial-building-icon.png";
                }
                //long value = (long)DataBinder.Eval(item.DataItem, "Population");
            }
        }
        //protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.ShowOuterBorders = CheckBox1.Checked;
        //}
        //protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.ShowTreeLines = CheckBox2.Checked;
        //}
        //protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    switch (DropDownList1.SelectedValue)
        //    {
        //        case "Both":
        //            RadTreeList1.GridLines = TreeListGridLines.Both;
        //            break;
        //        case "Horizontal":
        //            RadTreeList1.GridLines = TreeListGridLines.Horizontal;
        //            break;
        //        case "None":
        //            RadTreeList1.GridLines = TreeListGridLines.None;
        //            break;
        //        case "Vertical":
        //            RadTreeList1.GridLines = TreeListGridLines.Vertical;
        //            break;
        //    }
        //}
        //protected void CheckBox3_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.AllowPaging = CheckBox3.Checked;
        //    RadTreeList1.Rebind();
        //}
        //protected void CheckBox4_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.AllowSorting = CheckBox4.Checked;
        //    RadTreeList1.SortExpressions.Clear();
        //    RadTreeList1.Rebind();
        //}
        //protected void CheckBox5_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.ClientSettings.Selecting.AllowItemSelection = CheckBox5.Checked;
        //    RadTreeList1.ClearSelectedItems();
        //}
        //protected void CheckBox6_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadTreeList1.AllowMultiItemSelection = CheckBox6.Checked;
        //}
    }
    public class MyData
    {
        public static List<MyItem> GetData()
        {
            List<MyItem> list = new List<MyItem>();
            //list.Add(new MyItem("A", "Appetizers", "", null, true, ""));
            //list.Add(new MyItem("B", "Beverages", "", null, true, ""));
            //list.Add(new MyItem("C", "Cheese", "", null, false, ""));
            //list.Add(new MyItem("A1", "Southwestern Twisted Chips", "150 gr.", 6.79m, false, "A"));
            //list.Add(new MyItem("A2", "Top Shelf Combo Appetizer", "300 gr.", 9.49m, true, "A"));
            //list.Add(new MyItem("B1", "Sangria", "90 ml.", 6.49m, true, "B"));
            //list.Add(new MyItem("B2", "Margarita", "60 ml.", 7.39m, false, "B"));
            //list.Add(new MyItem("B3", "Red Cherry Boost", "200 ml.", 6.99m, false, "B"));
            //list.Add(new MyItem("B4", "Mojito", "180 ml.", 7.59m, true, "B"));
            //list.Add(new MyItem("C1", "Blue Cheese and Hazelnut Shortbread", "220 gr.", 10.69m, false, "C"));
            //list.Add(new MyItem("C2", "Avocado Feta Salsa", "240 gr.", 7.19m, false, "C"));

            BSCEntities context = new BSCEntities();
            List<KPI> kpis = new List<KPI>();
            kpis = (from k in context.KPIs.Include("Perspective")
                   select k).ToList();

            foreach (KPI k in kpis)
            {
                list.Add(new MyItem(k.KPI_ID, k.Name, k.Perspective.Name, k.Perspective_FK));
                //MyItem mi = new MyItem(k.KPI_ID, k.Name, k.Perspective.
            }

            List<Perspective> sts = new List<Perspective>();
            sts = (from k in context.Perspectives
                    select k).ToList();

            foreach (Perspective k in sts)
            {
                list.Add(new MyItem(k.Perspective_ID, k.Name, k.Name, 0));
                //MyItem mi = new MyItem(k.KPI_ID, k.Name, k.st
            }

            list.Add(new MyItem(0, "Balance Scorecard"));

            return list;
        }
    }
    public class MyItem
    {
        public string ID { get; set; }
        public string Name { get; set; }
        //public string Quantity { get; set; }
        //public decimal? Price { get; set; }
        //public bool InStock { get; set; }
        public string ParentID { get; set; }
        public string Persp { get; set; }

        public MyItem(int id, string name)
        {
            ID = id.ToString();
            Name = name;
            Persp = "";
            ParentID = ""; // parentID.ToString();
        }
        public MyItem(int id, string name, string perspective, int parentID)
        {
            if (parentID == 0) //perspective
            {
                ID = "perspective" + id.ToString();
                Name = name;
                Persp = perspective;
                ParentID = parentID.ToString();
            }
            else //kpi
            {
                ID = id.ToString();
                Name = name;
                //Quantity = quantity;
                //Price = price;
                //InStock = inStock;
                Persp = perspective;
                ParentID = "perspective" + parentID.ToString();
            }
        }
    
    }
}